import * as projectService from '../../services/design/projectServices';
import { notification } from 'antd';
export default {
    namespace: 'project',
    state: {
        title: '智能设计',
        current: null,
        projects: [],
        currentTreeData:[]
    },
    reducers: {
        addProject(state, { payload: project }) {
            return { ...state, projects: [...state.projects ? state.projects : [], project] };
        },
        setCurrent(state, { payload: project }) {
            return { ...state, current: project };
        },
        setCurrentTreeData(state, { payload: data }) {
            return { ...state, currentTreeData: data };
        }
    },
    effects: {
        *add({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(projectService.addProject, { ...values });
            if (res&&res.data && res.data.code == "200") {
                onSuccess(res);
                yield put({
                    type: 'setCurrent',
                    payload: res.data.data
                });
                notification.info({ message: "添加项目成功" });
                yield put({type: 'updateCurrent',payload: res.data.data});
            } else {
            
                onError(res&&res.data?res.data.code:'服务器未连接');
            }

        },
        *deleteProject({ payload: { params, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(projectService.deleteProject, params);

            if (res&&res.data && res.data.code == "200") {
                onSuccess(res);
                notification.info({ message: "项目删除成功" });
            } else {
              
                onError(res&&res.data?res.data.message:'服务器未连接');
            }

        },
        *getProjectsData({ payload: { params, onSuccess, onError } }, { call, put }) {
            /********获取所有项目***********/
            const res = yield call(projectService.getProjects, { ...params });
            if (res&&res.data && res.data.code == "200") {
                onSuccess(res.data.data?res.data.data:[]);

            } else {
                onError(res&&res.data?res.data.message:'服务器连接失败');
            }
        },
        *getProjectDetail({ payload: values }, { call, put, select }) {
            const res = yield call(projectService.getProjectDetail, { ...values });
            if (res && res.data && res.data.code == "200") {
                yield put({
                    type: 'setCurrent',
                    payload: res.data.data
                });
                return res.data.data
            } else {
                return null
            }
        },
        *updateCurrentTreeData({ payload: data }, { call, put,select,take }) {
            yield put({
                type: 'setCurrentTreeData',
                payload: data
            });
        },
        *updateCurrent({ payload: project }, { call, put,select,take }) {
            yield put({
                type: 'setCurrent',
                payload: project
            });
             //更新当前桥梁
             yield put({
                type: 'bridge/updateCurrent',
                payload: null
            });
            yield put({
                type: 'refRoads/updateCurrent',
                payload: null
            });
            yield put({
                type: 'refSpans/updatebuildingSpans',
                payload: null
            });
            yield new Promise((resolve, reject) => {
                resolve()
              });
            
        },
        
        *updateProjectInfo({ payload: { params, onSuccess, onError }  }, { call, put,select,take }) {
            const res = yield call(projectService.updateProject, { ...params });
            if (res&&res.data && res.data.code == "200") {
               
                onSuccess(res.data.data?res.data.data:[]);

            } else {
          
                onError(res&&res.data?res.data.message:'服务器连接失败');
            }
            
        }
    }
}